package com.hbzhit.modules.sys.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hbzhit.common.utils.R;
import com.hbzhit.common.utils.ShiroUtils;
import com.hbzhit.modules.sys.entity.ProEntity;
import com.hbzhit.modules.sys.entity.ProReportEntity;
import com.hbzhit.modules.sys.entity.ReportApprovalEntity;
import com.hbzhit.modules.sys.entity.SysUserEntity;
import com.hbzhit.modules.sys.service.ProReportService;
import com.hbzhit.modules.sys.service.ProService;
import com.hbzhit.modules.sys.service.ReportApprovalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 总监审批表
 *
 * @author hbzhit
 * @date 2022-05-05 09:00:04
 */
@Api(tags = {"总监审批表"})
@AllArgsConstructor
@RestController
@RequestMapping("sys/reportApproval")
public class ReportApprovalController {
    @Resource
    private ReportApprovalService reportApprovalService;
    @Resource
    private ProReportService proReportService;
    @Resource
    private ProService proService;

    @PostMapping("/audit")
    @ApiOperation("交付总监审核")
    @Transactional
    public R<?> audit(@RequestBody ReportApprovalEntity reportApproval) {
        // 获取当前用户
        SysUserEntity currUser = ShiroUtils.getUser();
        if (ObjectUtil.isEmpty(reportApproval.getProId()) || ObjectUtil.isEmpty(reportApproval.getGroupId()) ||
                ObjectUtil.isEmpty(reportApproval.getBpmStatus()) || ObjectUtil.isEmpty(reportApproval.getMessage()))
            return R.error().data("参数错误");
        reportApproval.setAuditTime(DateUtil.date());
        reportApproval.setUserId(currUser.getId());
        int status = 0;
        if (reportApproval.getBpmStatus() == 1) {
            status = 3;
        } else {
            status = 4;
        }
        ProEntity pro = new ProEntity();
        pro.setId(reportApproval.getProId());
        pro.setReportGroupId(reportApproval.getGroupId());
        proService.updateById(pro);
        proReportService.update(Wrappers.<ProReportEntity>update().lambda().set(ProReportEntity::getBpmStatus, status)
                .set(ProReportEntity::getApprover, currUser.getId()).set(ProReportEntity::getApproverTime, DateUtil.date())
                .eq(ProReportEntity::getProId, reportApproval.getProId()).eq(ProReportEntity::getReportGroupId, reportApproval.getGroupId()).eq(ProReportEntity::getReporterType, 2));
        return R.ok().data(reportApprovalService.update(reportApproval, new LambdaQueryWrapper<ReportApprovalEntity>()
                .eq(ReportApprovalEntity::getProId, reportApproval.getProId()).eq(ReportApprovalEntity::getGroupId, reportApproval.getGroupId())));
    }
}
